﻿@page "/rotation"
@using VectSharp;
@using VectSharp.SVG;

<style>
	table {
		table-layout: fixed;
	}

	td {
		width: 400px;
		padding: 10px;
	}
</style>

<div style="width: 100vw; height: 100vh; position: relative;">
	<div style="width: calc(100% - 400px); height: 100%; position: absolute; top: 0; left: 0; text-align: center">
		<img src="@imgSource" style="max-width: 100%; max-height: 100%; margin-top: 50vh; transform: translate(0, -50%)" />
	</div>

	<div style="width: 400px; height: 100vh; position: absolute; top: 0; right: 0;">
		<table style="margin-top: 50vh; transform: translate(0, -50%)">
			<tr>
				<td>
					<div style="display: inline-block; position: relative; width: 380px">
						<span class="mdc-floating-label mdc-floating-label--float-above" style="margin-left:0.5em; margin-top: 2em; color: rgba(0, 0, 0, 0.6)">Rotation angle</span><br />
						<div style="position: relative">
							<div style="display: inline-block; width: 300px">
								<MatSlider ValueMin="0" ValueMax="100" @bind-Value="@angle" Immediate="true"></MatSlider>
							</div>
							<span style="position: absolute; top: 50%; left: 320px; transform: translate(0, -50%)">@((angle / 100 * 2 * Math.PI).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture))</span>
						</div>
					</div>
				</td>
			</tr>
		</table>
	</div>
</div>

@code {
	private double _angle = 12.5;
	private double angle
	{
		get
		{
			return _angle;
		}

		set
		{
			_angle = value;
			Render();
		}
	}

	private string imgSource = "";

	protected override Task OnInitializedAsync()
	{
		Render();
		return Task.CompletedTask;
	}

	public void Render()
	{
		Page page = new Page(120, 120);
		Graphics graphics = page.Graphics;

		Font fnt = new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.TimesItalic), 6);

		graphics.Translate(50, 50);

		graphics.Scale(2, 2);

		graphics.StrokePath(new GraphicsPath().MoveTo(0, 0).LineTo(10, 0), Colour.FromRgb(127, 184, 216));
		graphics.FillPath(new GraphicsPath().MoveTo(12, 0).LineTo(9, 2).LineTo(9, -2).Close(), Colour.FromRgb(127, 184, 216));
		graphics.StrokePath(new GraphicsPath().MoveTo(0, 0).LineTo(0, 10), Colour.FromRgb(234, 174, 127));
		graphics.FillPath(new GraphicsPath().MoveTo(0, 12).LineTo(2, 9).LineTo(-2, 9).Close(), Colour.FromRgb(234, 174, 127));
		graphics.FillPath(new GraphicsPath().Arc(0, 0, 1, 0, 2 * Math.PI), Colour.FromRgb(220, 220, 220));

		graphics.Rotate(angle / 100 * 2 * Math.PI);

		graphics.StrokePath(new GraphicsPath().MoveTo(0, 0).LineTo(10, 0), Colour.FromRgb(0, 114, 178));
		graphics.FillPath(new GraphicsPath().MoveTo(12, 0).LineTo(9, 2).LineTo(9, -2).Close(), Colour.FromRgb(0, 114, 178));
		graphics.StrokePath(new GraphicsPath().MoveTo(0, 0).LineTo(0, 10), Colour.FromRgb(213, 94, 0));
		graphics.FillPath(new GraphicsPath().MoveTo(0, 12).LineTo(2, 9).LineTo(-2, 9).Close(), Colour.FromRgb(213, 94, 0));
		graphics.FillPath(new GraphicsPath().Arc(0, 0, 1, 0, 2 * Math.PI), Colour.FromRgb(180, 180, 180));
		graphics.FillText(15 - fnt.MeasureText("x").Width * 0.5, 0, "x", fnt, Colours.Black, TextBaselines.Middle);
		graphics.FillText(- fnt.MeasureText("y").Width * 0.5, 16, "y", fnt, Colours.Black, TextBaselines.Middle);

		using (MemoryStream ms = new MemoryStream())
		{
			page.SaveAsSVG(ms);
			ms.Seek(0, SeekOrigin.Begin);

			using (StreamReader sr = new StreamReader(ms))
			{
				this.imgSource = "data:image/svg+xml;base64," + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sr.ReadToEnd()));
			}
		}
	}
}
